from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import pymysql
import time
import pymysql
from ext import db

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class TodoList(db.Model):
    __tablename__ = 'todolist'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False)
    equipmnet = db.Column(db.String(256), nullable=True)
    xinghao = db.Column(db.String(256), nullable=False)
    bumen = db.Column(db.Integer, nullable=False)
    owner = db.Column(db.String(256), nullable=False)
    dispaly = db.Column(db.String(256), nullable=False)
    cpu = db.Column(db.String(256), nullable=False)
    memory = db.Column(db.String(256), nullable=False)
    disk = db.Column(db.String(256), nullable=False)
    mac_addr = db.Column(db.String(256), nullable=False)
    guzhang = db.Column(db.String(4), nullable=True)
    create_time = db.Column(db.String(256), nullable=True)


# 初始化数据库连接:
engine = create_engine("mysql+pymysql://xiang:xiang2018@127.0.0.1/test")
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)

# 创建session对象:
session = DBSession()
# 创建新User对象:
#new_user = User(id='5', name='Bob')
# 添加到session:
#session.add(new_user)
# 提交即保存到数据库:
#session.commit()
# 关闭session:
#session.close()

# 创建Session:
session = DBSession()
# 创建Query查询，filter是where条件，最后调用one()返回唯一行，如果调用all()则返回所有行:

user = session.query(TodoList).filter(TodoList.id=='1').feachall()
# 打印类型和对象的name属性:
print('type:', type(TodoList))
print('user:', user)
for i in user :
    print(i)
# 关闭Session:
session.close()